package com.superteam.zzl.view;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import com.superteam.zzl.activity.MainActivity;
import com.superteam.zzl.activity.R;
import com.superteam.zzl.view.fragment.*;

import java.util.ArrayList;

/**
 * Created by zhang on 2017/1/6.
 */

/**
 * fragment切换控制器 用于底部导航切换Fragment
 */

public class FragmentCotroller {
    int containerId;//占位布局id
    private FragmentManager fm; //碎片管理者对象
    private static FragmentCotroller controller;//碎片切换控制器对象
    private ArrayList<com.superteam.zzl.view.BaseFragment> fragments; //碎片集合
    public  String[] title; //每个Fragment对应的底部导航的名称，存于strings中

    /**
     * 私有构造方法  （实例化一些对象）
     * @param containerId  占位布局id
     * @param mainActivity 挂载的Activity对象
     */
    private FragmentCotroller(int containerId, MainActivity mainActivity) {
        this.containerId = containerId;
        fm = mainActivity.getSupportFragmentManager();
        title = mainActivity.getResources().getStringArray(R.array.native_main);
        initFragment();
    }

    /**
     * 外界获取碎片切换对象的的方法
     * @param containerId
     * @param mainActivity
     * @return
     */
    public static FragmentCotroller getInstance(int containerId,MainActivity mainActivity) {
        if (controller == null) {
            controller = new FragmentCotroller(containerId,mainActivity);
        }
        return controller;
    }

    /**
     * 初始化碎片
     */
    private void initFragment() {
        if(fragments == null) {

            fragments = new ArrayList<>();
            fragments.add(MainRecommendFragment.getInstance(title[0]));
            fragments.add(MainDestinationFragment.getInstance(title[1]));
            fragments.add(MainStoreFragment.getInstance(title[2]));
            fragments.add(MainMyDynamicFragment.getInstance(title[3]));
            fragments.add(MainMineFragment.getInstance(title[4]));
        }
        FragmentTransaction ft = fm.beginTransaction();
        //ft.commitAllowingStateLoss();
        for(Fragment fragment : fragments) {
            if(fragment.isAdded()) {
                ft.replace(containerId, fragment);
            }else{
                ft.add(containerId,fragment);
            }
        }
//        ft.commitAllowingStateLoss();
//        fragments = new ArrayList<>();
//        fragments.add(MainRecommendFragment.getInstance(title[0]));
//        fragments.add(MainDestinationFragment.getInstance(title[1]));
//        fragments.add(MainStoreFragment.getInstance(title[2]));
//        fragments.add(MainMyDynamicFragment.getInstance(title[3]));
//        fragments.add(MainMineFragment.getInstance(title[4]));
//        FragmentTransaction ft = fm.beginTransaction();
//        for(Fragment fragment : fragments) {
//            ft.add(containerId, fragment);
//        }
        ft.commit();
    }

    /**
     * 显示碎片
     * @param position 要显式的碎片在碎片集合中的索引
     */
    public void showFragment(int position) {
        hideFragment();
        FragmentTransaction ft = fm.beginTransaction();
        ft.show(fragments.get(position));
        ft.commit();
    }

    /**
     * 隐藏碎片
     */
    public void hideFragment() {
        FragmentTransaction ft = fm.beginTransaction();
        for(Fragment fragment : fragments) {
            if(fragment != null)
                ft.hide(fragment);
        }
        ft.commit();
    }
}
